#include "server.h"
#include "network.h"

ClientToServe ctos;

void* run(void* arg)
{
	NetWork* clinw = (NetWork*)arg;
	char buf[BUFLEN] = {};

	while(true)
	{
		nrecv(clinw,buf,sizeof(buf));
		changeToStruct(buf,&ctos);

		switch(ctos.type)
		{
			case M_OPEN: 	 openAccount(buf); 		break;
			case M_LOGIN:	 login(buf); 	 		break;
			case M_DESTROY:	 destroy(buf);  		break;
			case M_UNLOCK:	 unlock(buf);		 	break;
			case M_SAVE:	 save(buf);				break;
			case M_TAKE:	 take(buf);	 			break;
			case M_QUERY:	 query(buf);	 		break;
			case M_TRANSFER: transfer(buf); 		break;
			case M_REPASS:	 resetPassword(buf);	break;
		}
		
		nsend(clinw,buf,strlen(buf)+1);
	}
}

int main()
{
	//初始化银行卡号
	initCardNo(BANK_PATH,132400000);

	NetWork* svrnw = nopen(SOCK_STREAM,"172.20.10.2",7777,true);

	while(true)
	{
		pthread_t tid;
		NetWork* clinw = naccept(svrnw);
		pthread_create(&tid,NULL,run,clinw);
	}
}
